<?php

class RealitySearch extends AbstractBox {
	
	protected $template = 'reality/search.tpl';

	protected $pageList = array();
	
	protected function setup() {
		$pageRepository = new PageRepository();
		if (Utils::post('btnsearch')) {
			$pageUid = intval(Utils::post('selectbox'));
			$page = $pageRepository->getOneByUid($pageUid);
			$url = $GLOBALS['TSFE']->config['config']['baseURL'] . $page->getUrl();
			$urlParams = array();
			if (Utils::post('price_from') != 'Cena od') {
				$urlParams[] = 'price_from=' . Utils::post('price_from');
			}
			if (Utils::post('price_to') != 'Cena do') {
				$urlParams[] = 'price_to=' . Utils::post('price_to');
			}
			if (Utils::post('area_from') != 'Výmera od (m2)') {
				$urlParams[] = 'area_from=' . Utils::post('area_from');
			}
			if (Utils::post('area_to') != 'Výmera do (m2)') {
				$urlParams[] = 'area_to=' . Utils::post('area_to');
			}
			if ($urlParams) {
				$url .= '?' . implode('&', $urlParams);
			}
			Utils::redirect($url, FALSE);
		}
		
		$this->getAllPagesRecursive(array(Page::PREDAJ));
		
		// TODO na tomto mieste treba spravit query na pozemky lukavica ak tiez budu mat byt v search dropdowne
		
		MySmarty::assign('pages', $this->pageList);
		MySmarty::assign('actualState', Utils::get());
	}
	
	protected function getAllPagesRecursive($parentList) {
		$pageRepository = new PageRepository();
		$pages = $pageRepository->getByPid($parentList);
		
		if ($pages) {
			$pageUidList = array();
			foreach ($pages as $page) {
				$this->pageList[] = $page;
				$this->getAllPagesRecursive($page['uid']);
			}
		}
	}
}

?>